<?php
/**
 * Created by PhpStorm.
 * User: wenquan
 * Date: 2017/8/15
 * Time: 下午1:54
 */

/**
 * @param $cinema_code
 * 获取影厅编码
 * 16位定长
 */
function getScreenCode($cinema_code){
    return $cinema_code.date('His').rand(10,99);
}

/**
 * @return string
 * 获取座位编码
 */
function getSeatCode($screen_code,$x_coord,$y_coord){
    $str = md5($screen_code.$x_coord.$y_coord.'');
    return substr($str,0,16);
}


/**
 * @return string
 * 获取场次编码
 */
function get_session_code(){
    return unique_rand(16);
}

/**
 * @return string
 * 获取排片编码
 */
function get_showtime_schedule_code(){
    return unique_rand(16);
}


/**
 * @return string
 * 获取放映计划编码
 */
function get_session_seat_code(){
    return unique_rand(16);
}

/**
 * @return string
 * 获取订单编码
 */
function get_order_code(){
    return unique_rand(16);
}

/**
 * @return string
 * 获取电影票编码
 */
function get_film_ticket_information(){
    return unique_rand(20);
}

/**
 * @return string
 *出票号
 */
function get_print_no(){
    return unique_rand(16);
}

/**
 * @return string
 * 出票验证码
 */
function get_verify_code(){
    return unique_rand(16);
}

/**
 * @return string
 * 结算上报code码
 */
function get_order_original_code(){
    return unique_rand(16);
}

/**
 * @param $code
 * @return \Illuminate\Support\Collection
 * 根据code码获取城市名称
 */
function get_city_name($code){
    return DB::table('citys')->where(['code'=>$code])->pluck('name')->first();
}

/**
 * @param $option_type
 * @return \Illuminate\Support\Collection
 * 获取选择项
 */
function get_options($option_type){
    return \App\Option::where(['options_type'=>$option_type])->pluck('options_value','options_key');
}

/**
 * @param $cinema_code
 * @return string
 * 获取网售商公钥
 */
function get_onilne_film_ticketer_publishkey($cinema_code){
    return unique_rand(8);
}

/**
 * @param $message
 * @return array
 * 失败
 */
function error($message,$data=''){
    return ['status'=>'error','message'=>$message,'data'=>$data];
}

/**
 * @param $data
 * @param $message
 * @return array
 * 成功
 */
function success($data,$message='',$attributes = array()){
    return ['status'=>'success','message'=>$message,'data'=>$data,'attrabutes'=>$attributes];
}

/**
 * @param $min
 * @param $max
 * @param $num
 * @return array|bool
 * 生成uuid
 */
function unique_rand($num) {
    $str = md5(uniqid(date('YmdHis'), true));
    $uuid  = substr($str,0,$num);
    return $uuid;
}

/**
 * @param string $tableName
 * @param array $multipleData
 * @return bool|int
 * 批量更新数据
 */
function updateBatch($tableName = "", $multipleData = array()){

    if( $tableName && !empty($multipleData) ) {

        // column or fields to update
        $updateColumn = array_keys($multipleData[0]);
        $referenceColumn = $updateColumn[0]; //e.g id
        unset($updateColumn[0]);
        $whereIn = "";

        $q = "UPDATE ".$tableName." SET ";
        foreach ( $updateColumn as $uColumn ) {
            $q .=  $uColumn." = CASE ";

            foreach( $multipleData as $data ) {
                $q .= "WHEN ".$referenceColumn." = ".$data[$referenceColumn]." THEN '".$data[$uColumn]."' ";
            }
            $q .= "ELSE ".$uColumn." END, ";
        }
        foreach( $multipleData as $data ) {
            $whereIn .= "'".$data[$referenceColumn]."', ";
        }
        $q = rtrim($q, ", ")." WHERE ".$referenceColumn." IN (".  rtrim($whereIn, ', ').")";

        // Update
        return DB::update(DB::raw($q));

    } else {
        return false;
    }
}

/**
 * @param $url 请求网址
 * @param bool $params 请求参数
 * @param int $ispost 请求方式
 * @param int $https https协议
 * @return bool|mixed
 */
function curl($url, $params = false, $ispost = 0, $https = 0)
{
    $httpInfo = array();
    $ch = curl_init();
    $headers = [];
    $headers['Content-type']="application/x-www-form-urlencoded";
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    if ($https) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
    }
    if ($ispost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        if ($params) {
            if (is_array($params)) {
                $params = http_build_query($params);
            }
            curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }

    $response = curl_exec($ch);

    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $response;
}

function logs($user,$msg){
    $manager_log = new \App\ManagerLogs();
    $manager_log->user_name = $user['data']['name'];
    $manager_log->user_id = $user['data']['id'];
    $manager_log->type = $user['data']['type'];
    $manager_log->cinema_code = $user['data']['cinema_code'];
    $manager_log->msg = $msg;
    $manager_log->date_time = date('Y-m-d H:i:s');
    $manager_log->cinema_name = $user['data']['cinema_code']==''?'院线':\App\Cinema::where('code','=',$user['data']['cinema_code'])->value('name');
    $manager_log->save();
}

/**
 * @param $month
 * @param $year
 * @return int
 * 获取某个月天数
 */
function getMonthLastDay($month, $year) {
    switch ($month) {
        case 4 :
        case 6 :
        case 9 :
        case 11 :
            $days = 30;
            break;
        case 2 :
            if ($year % 4 == 0) {
                if ($year % 100 == 0) {
                    $days = $year % 400 == 0 ? 29 : 28;
                } else {
                    $days = 29;
                }
            } else {
                $days = 28;
            }
            break;
        default :
            $days = 31;
            break;
    }
    return $days;
}



